package com.yuhao.common.strcount;

import com.yuhao.common.strcount.entity.Test;
import com.yuhao.common.strcount.entity.Test1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static final String strReg = "rows";

    public static final String strReg1 = "行";

    public static void main(String[] args) {
        /**第一组
        String[] strs = {
         "\"ZW_WYJRQ\"",
         "\"FW_YYT\"",
         "\"ZW_WYJJJR\"",
         "\"FW_TDKHMX\"",
         "\"FW_TDJH\"",
         "\"FW_XXFBSQ\"",
         "\"FW_ZSD\"",
         "\"FW_ZSWT\"",
         "\"FW_TFDGDXX\"",
         "\"FW_TDSJ\"",
         "\"FW_TDSB\"",
         "\"FW_ZSFL\"",
         "\"FW_ZSDGL\"",
         "\"W_TDTZPGXX\"",
         "\"FW_TDTZJL\"",
         "\"FW_FBQDXX\"",
         "\"FW_KFDJXX\"",
         "\"FW_ZSDSQXX\"",
         "\"FW_ZSDA\""
        };*/
        /** 第二组
         String[] strs = {
         "\"HS_DFTZDMXXX\"",
         "\"ZW_PJDYXX\"",
         "\"ZW_QFJL\"",
         "\"ZW_QFMX\"",
         "\"ZW_WYJMX\"",
         "\"HS_DFTZRZ\"",
         "\"HS_LJFMX\"",
         "\"LC_CBXX\"",
         "\"ZW_YSJH\"",
         "\"ZW_YSJL\"",
         "\"ZW_YGDCZXX\""
         };*/
        /** 第三组
         String[] strs = {
         "\"ZW_SSDFJL\"",
         "\"ZW_SSDFMX\"",
         "\"ZW_YSDFJL\"",
         "\"ZW_YSDFMX\""
         };*/

        // new
        /* 第一组*/
        String[] strs =  {
                "\"BILL_PENALTY_DATE\"",
                "\"CUSTSERV_BUSINESS_HALL_03\"",
                "\"BILL_PENALTY_HOLIDAY\"",
                "\"CUSTSERV_POW_FAIL_DETAIL_03\"",
                "\"CUSTSERV_POW_FAIL_PLAN_03\"",
                "\"CUSTSERV_INFO_PUBL_APPL_03\"",
                "\"CUSTSERV_KNOW_POINT_03\"",
                "\"CUSTSERV_KNOW_QUS_03\"",
                "\"CUSTSERV_POWFAIL_WORKORD_03\"",
                "\"CUSTSERV_POW_FAIL_EVENT_03\"",
                "\"CUSTSERV_POW_FAIL_EQU_03\"",
                "\"CUSTSERV_KNOW_CLASSIFY_03\"",
                "\"CUSTSERV_KNOW_RELAT_03\"",
                "\"CUSTSERV_POW_FAIL_DISP_03\"",
                "\"CUSTSERV_BLACKOUT_NOTIF_03\"",
                "\"CUSTSERV_INFO_PUBL_CHAN_03\"",
                "\"CUSTSERV_TARIFF_INFO_03\"",
                "\"CUSTSERV_KNOW_APPL_INFO_03\"",
                "\"CUSTSERV_KNOW_ANS_03\""
        };
        List<String> list = toArrayByFileReader1("D:/数据迁移日志/kf20191219_import.log");
//        Integer[] o = (Integer[])Array.newInstance(Integer.class, 10);
//        o[0] = 1;
        Test test = Test.builder().DDXCSJ("1").build();
        Test1 test2 = new Test1.Builder(2).build();
        Test1 test1 = new Test1.Builder(1).code(2).build();
//        List<MainTest3> mainTest2s =   list.stream().map(e -> new MainTest3(e)).collect(Collectors.toList());
//        print(mainTest2s);
        Map<String, String> map = new HashMap<>();
        DecimalFormat decimalFormat = new DecimalFormat("#.0000");
        for (String str : strs) {
            long index = 0L;
            BigDecimal bigDecimal = BigDecimal.valueOf(0.00);
            int j = 0;
            for (int i = 0; i < list.size(); i++) {
                String e = list.get(i);
                if (e.indexOf(str) != -1) {
                    long strLong = 0;
                    BigDecimal strBigD = new BigDecimal(0);
                    if (e.indexOf("GB") != -1) {
                        strLong = subStrToLong(e, "GB", strReg);
                        strBigD = BigDecimal.valueOf(subStrToDouble(e, "\"  ", "GB") * 1024);
                    } else if (e.indexOf("MB") != -1) {
                        strLong = subStrToLong(e, "MB", strReg);
                        strBigD = BigDecimal.valueOf(subStrToDouble(e, "\"  ", "MB"));
                    } else if (e.indexOf("KB") != -1) {
                        strLong = subStrToLong(e, "KB", strReg);
                        strBigD = BigDecimal.valueOf(subStrToDouble(e, "\"  ", "KB") / 1024);
                    }
                    bigDecimal = bigDecimal.add(strBigD);
                    index += strLong;
                }
            }
            System.out.println(str+",rows:" + index + ", size:" + decimalFormat.format(bigDecimal.doubleValue()));
            map.put(str, "rows:" + index + ", size:" + decimalFormat.format(bigDecimal.doubleValue()));
        }
        for (Map.Entry<String, String> m : map.entrySet()) {
//            System.out.println("K:" + m.getKey() + ", V:" + m.getValue());
        }
    }


    public static List<String> toArrayByFileReader1(String name) {
        // 使用ArrayList来存储每行读取到的字符串
        List<String> arrayList = new ArrayList<>();
        try (
                FileReader fr = new FileReader(name);
                BufferedReader bf = new BufferedReader(fr);
        ) {
            String str;
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public static long subStrToLong(String str, String start, String end) {
        int startIndex = str.indexOf(start);
        int endIndex = str.indexOf(end);
        String result = str.substring(startIndex + 2, endIndex).trim();
        return Long.parseLong(result);
    }

    public static double subStrToDouble(String str, String start, String end) {
        int startIndex = str.indexOf(start);
        int endIndex = str.indexOf(end);
        String result = str.substring(startIndex + 1, endIndex).trim();
        return Double.parseDouble(result);
    }

    public static <T extends MainInterface<? super T>> void print(List<T> list) {
        list.stream().forEach(System.out::println);
    }
}
